package com.qing.administer.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qing.administer.annotations.ResponseResult;
import com.qing.administer.annotations.WebLog;
import com.qing.administer.entity.SysMenu;
import com.qing.administer.entity.SysRole;
import com.qing.administer.pojo.dto.SysRoleDTO;
import com.qing.administer.pojo.vo.SysRoleVO;
import com.qing.administer.service.SysMenuService;
import com.qing.administer.service.SysRoleMenuService;
import com.qing.administer.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author qing
 * @since 2021-09-17
 */
@Api(tags = "角色管理")
@ResponseResult
@RestController
@RequestMapping("/administer/sys-role")
public class SysRoleController {

	@Autowired
	private SysRoleService sysRoleService;

	@Autowired
	private SysMenuService sysMenuService;

	@Autowired
	private SysRoleMenuService sysRoleMenuService;

	@WebLog("列表")
	@ApiOperation(value = "列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "current",value = "当前页",required = true,defaultValue = "1"),
			@ApiImplicitParam(name = "size",value = "每页显示条数",required = true,defaultValue = "10"),
			@ApiImplicitParam(name = "keyword",value = "模糊匹配",required = false)
	})
	@GetMapping("/page")
	public IPage<SysRole> getPage(@RequestParam(value = "current",defaultValue = "1",required = false) Long current,
								  @RequestParam(value = "size",defaultValue = "10",required = false) Long size,
								  @RequestParam(value = "keyword",defaultValue = "",required = false) String keyword){

		return sysRoleService.findPage(current,size,keyword);
	}


	@WebLog("编辑")
	@ApiOperation(value = "编辑")
	@PostMapping
	public Boolean edit(@RequestBody SysRoleDTO sysRoleDTO){
		SysRole sysRole=new SysRole();
		BeanUtils.copyProperties(sysRoleDTO,sysRole);
		sysRoleService.saveOrUpdate(sysRole);
		return sysRoleMenuService.editSysRoleMenu(sysRole.getRoleId(),sysRoleDTO.getMenuIds());

	}


	@WebLog("删除")
	@ApiOperation(value = "删除")
	@ApiImplicitParam(name = "id",value = "角色Id",required = true)
	@DeleteMapping
	public Boolean delete(@RequestParam(value = "id") Long id){
		return sysRoleService.removeById(id);
	}


	@WebLog("详情")
	@ApiOperation(value = "详情")
	@ApiImplicitParam(name = "roleId",value = "角色Id",required = true)
	@GetMapping("/detail")
	public SysRoleVO getDetail(@RequestParam("roleId") Long roleId){
		SysRoleVO sysRoleVO=new SysRoleVO();
		BeanUtils.copyProperties(sysRoleService.getById(roleId),sysRoleVO);
		sysRoleVO.setSysMenuList(sysMenuService.selectSysMenuByRoleId(roleId));
		return  sysRoleVO;
	}
}

